from xml.etree import ElementTree as ET

'''
# 创建根节点
root = ET.Element('home')

# 创建节点大儿子
son1 = ET.Element('son',{'name':'son1'})
# 创建节点小儿子
son2 = ET.Element('son',{'name':'son2'})

# 在大儿子中创建两个孙子
grandson1 = ET.Element('grandson',{'name':'grandson1'})
grandson2 = ET.Element('grandson',{'name':'grandson2'})
son1.append(grandson1)
son1.append(grandson2)

# 把大儿子添加到根节点中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('ooo.xml',encoding='utf-8',short_empty_elements=False)   # 生成xml的格式的时候是否用短标签
tree.write('ooo.xml',encoding='utf-8',short_empty_elements=True)   # 生成xml的格式的时候是否用短标签
'''

'''
root = ET.Element('family')
# 创建大儿子
son1 = ET.Element('son',{'name':'son1'})
# 创建节点小儿子
son2 = ET.Element('son',{'name':'son2'})

# 在大儿子汇总创建两个孙子
grandson1 = son1.makeelement('grandson',{'name':'grandson1'})
grandson2 = son1.makeelement('grandson',{'name':'grandson2'})
son1.append(grandson1)
son1.append(grandson2)

# 把儿子添加到根节点中
root.append(son1)
root.append(son2)

tree = ET.ElementTree(root)
tree.write('oo1.xml',encoding='utf-8')
'''

'''
# 创建根节点
root = ET.Element('home')

# 创建节点大儿子
son1 = ET.SubElement(root,'son',attrib={'name':'son1'})
# 创建节点小儿子
son2 = ET.SubElement(root,'son',attrib={'name':'son2'})

# 在大儿子中创建一个孙子
grandson1 = ET.SubElement(son1,'age',attrib={'name':'grandson1'})
grandson1.text = '孙子'

# 生成文档对象
et = ET.ElementTree(root)
et.write('test.xmk',encoding='utf-8')
'''

# xml转字典
content = '''
<data></data>
'''
info = {}
root = ET.XML(content)
for node in root:
    print(node.tag,node.text)
    info[node.tag] = node.text

print(info)